home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / tvmacro.exe / MACROS.PAS < prev    next >
Pascal/Delphi Source File  |  1992-01-31  |  20KB  |  701 lines

  1. unit Macros;
  2.  
  3. (***************************************************************************)
  4. (*                          Turbo Vision Macros                            *)
  5. (*                       $25 Shareware, Version 1.0                        *)
  6. (*                                                                         *)
  7. (*            (c) Copyright 1992 Cybersoft & Lawrence V. Koepke            *)
  8. (*                          All Rights Reserved                            *)
  9. (*                                                                         *)
  10. (*                               Cybersoft                                 *)
  11. (*                   1921 Minto Dr., San Jose, CA 95132                    *)
  12. (*                             (408) 272-2927                              *)
  13. (***************************************************************************)
  14.  
  15. {$X+}
  16. {.$DEFINE TESTING}
  17.  
  18. {
  19. Turbo Vision Macros is a complete Event Macro Handler, and is released as
  20. Shareware. If you find it useful, please support my efforts. I am an
  21. independant developer. I have no job... I support the wife and kids with my
  22. wits and perseverance to succeed. If enough people support my efforts with
  23. TV Macros, I may even release future versions that support advanced features
  24. (i.e. Loops, If-Then, etc.)! I encourage you to pass this code on to your
  25. friends and colleagues, for, if nothing else, I'm sure that it will make some
  26. people's jobs easier; but, please, do not pass on modified code... let's
  27. have some version control here, send me your modifications.
  28.  
  29. This unit Replaces TApp.GetEvent with a method having the following features:
  30. - Records events as macros
  31. - Plays back events in macros
  32. - Both Mouse and Keyboard supported
  33. - Macros can record the playback of other macros
  34. - Macros are selected from a pick-list for playback
  35. - Macros are given a name up to 50 characters long
  36. - Halt playback with Escape and confirmation (requires MsgBox)
  37. - Adds app. 16K to the application (with integrated debugger information)
  38. - All non-current macros are kept on disk, macros are only loaded when run
  39. - Does NOT replace BIOS keyboard interrupt 16 or 9
  40. - Macros are stored in two files : MACROS.NDX and MACROS.MAC.
  41.  
  42. Macros requires units Lists and Picks (also from Cybersoft) to compile.
  43. }
  44.  
  45. interface
  46. uses App, Drivers, Picks, Lists, StdDlg, Objects, Dialogs;
  47.  
  48. type
  49.      (*-------- The basic App type to include macros ------------------*)
  50.  
  51.      PMacApp = ^TMacApp;
  52.      TMacApp = object (TApplication)
  53.        constructor Init;
  54.        destructor Done; virtual;
  55.        procedure GetEvent (var Event : TEvent); virtual;
  56.      end;
  57.  
  58.      (*-------- The Macro Dialog (replaces event handler) ----------*)
  59.  
  60.      PMacDialog = ^TMacDialog;
  61.      TMacDialog = object (TDialog)
  62.        procedure HandleEvent (var Event : TEvent); virtual;
  63.      end;
  64.  
  65.      (*-------- Macro file record ----------------------------------*)
  66.  
  67.      AMacroRecord = TEvent;                  {used to define file record}
  68.  
  69.  
  70.      (*-------- Macro Index file record ----------------------------*)
  71.  
  72.      AMacroIndex = Record
  73.                      Name   : String [50];
  74.                      Start,
  75.                      Length : Integer;
  76.                    end;
  77.  
  78.  
  79.      (*-------- The Macro ------------------------------------------*)
  80.      { Each macro is a collection of Events of type TEvent. }
  81.  
  82.      PMacro = ^TMacro;
  83.      TMacro = object (TQueue)
  84.      end;
  85.  
  86.  
  87.      (*------- A stack of macros. ----------------------------------*)
  88.      { Used to store interrupted macros (ones that call
  89.        other macros. (A Stack of Queues, so to speak.) }
  90.  
  91.      PMacroStack = ^TMacroStack;
  92.      TMacroStack = object(TStack)
  93.        procedure PushMacro (Macro : PMacro);
  94.      end;
  95.  
  96.      (*------- The macro picklist ----------------------------------*)
  97.  
  98.      (* - - - - - - - - -- - - - - - -- - - - - *)
  99.  
  100.      { Used for Sorted macro list. }
  101.      TSortRecord = record
  102.        Name : String [50];
  103.        RecNUm : integer;
  104.      end;
  105.  
  106.      PMacroList = ^TMacroList;
  107.      TMacroList = object (TSortedCollection)
  108.        function Compare (Key1, Key2 : Pointer): Integer; virtual;
  109.        procedure FreeItem (Item : Pointer); virtual;
  110.      end;
  111.  
  112.      PMacroListBox = ^TMacroListBox;
  113.      TMacroListBox = object (TSortedListBox)        {from StdDlg}
  114.        procedure HandleEvent (var Event : TEvent); virtual;
  115.        function GetText (Item : Integer; MaxLen : Integer) : String; virtual;
  116.      end;
  117.  
  118.      (*-------------------------------------------------------------*)
  119.      PEvent = ^TEvent;
  120.  
  121.  
  122. procedure StartRecording;
  123. procedure StopRecording;
  124. procedure StartPlayback;
  125. procedure StopPlayback;
  126. procedure DeleteMacro; {Can this be disabled during Recording or playback?}
  127.  
  128. implementation
  129. uses Views, Strings, Crt, MsgBox;
  130.  
  131. type PSortRecord = ^TSortRecord;
  132.  
  133. var
  134.    MacroFile          : file of AMacroRecord;      {file of macros}
  135.    MacroFileIndex     : file of AMacroIndex;       {file of indexes to macros}
  136.    MacFileName        : string;                    {file name root; no ext.}
  137.    RecordMacIndex     : AMacroIndex;               {1 index record}
  138.    MacPickList        : PPickList;                 {picklist of macros}
  139.    MacStack           : PMacroStack;               {collection of macros}
  140.    InRecording,
  141.    InPlayback         : boolean;                   {states}
  142.    PtrEvent           : PEvent;                    {used only in GetEvent}
  143.    OurMacro           : PMacro;                    {the current macro}
  144.    CheckHalt          : boolean;                   {allows macro interruption}
  145.  
  146.  
  147.  
  148. (* ------------------------- The Macro Files ---------------------------- *)
  149.  
  150. function OpenMacroFiles (Filename : string): boolean;
  151. var ok : boolean;
  152. begin
  153.   ok := false;
  154. {$I-}
  155.   Assign (MacroFile, Filename + '.MAC');
  156.   Reset (MacroFile);
  157.   ok := IOResult = 0;
  158.   if not ok then
  159.   begin
  160.     Rewrite (MacroFile);
  161.     ok := IOResult = 0;
  162.     if not ok then
  163.       MessageBox('Couldn''t open or create macro data file.',
  164.                  nil, mfOKButton);
  165.   end;
  166.  
  167.   if ok then
  168.   begin
  169.     Assign (MacroFileIndex, Filename + '.NDX');
  170.     Reset (MacroFileIndex);
  171.     ok := IOResult = 0;
  172.     if not ok then
  173.     begin
  174.       Rewrite (MacroFileIndex);
  175.       ok := IOResult = 0;
  176.       if not ok then
  177.         MessageBox('Couldn''t open or create macro index file.',
  178.                    nil, mfOKButton);
  179.     end;
  180.   end;
  181.  
  182.   OpenMacroFiles := ok;
  183. {$I+}
  184. end;
  185.  
  186.  
  187. procedure CloseMacrofiles;
  188. begin
  189.   Close (MacroFile);
  190.   Close (MacroFileIndex);
  191. end;
  192.  
  193.  
  194.  
  195. (* ------------------------ The Macro Dialog Box ------------------------- *)
  196.  
  197. {This HandleEvent replaces the space with an underscore because StdDlg's
  198.  TSortedListBox does not recognize spaces with alphanumeric searches for
  199.  the list items. This HandleEvent also converts characters to upper-case,
  200.  since TSortedListBox is case-sensitive.}
  201. procedure TMacDialog.HandleEvent (var Event : TEvent);
  202. begin
  203.   if Event.What = evKeyDown then
  204.     if Event.CharCode = #32 then
  205.       Event.CharCode := #95
  206.     else
  207.       Event.CharCode := UpCase(Event.CharCode);
  208.   TDialog.HandleEvent (Event);
  209. end;
  210.  
  211.  
  212. FUNCTION MakeDialog : PMacDialog;
  213. var
  214.   Dlg : PMacDialog;
  215.   R : TRect;
  216.   Control, Labl, Histry : PView;
  217. Begin
  218. R.Assign(4,6,76,13);
  219. New(Dlg, Init(R, 'Macro'));
  220.  
  221. R.Assign(17,2,69,3);
  222. Control := New(PInputLine, Init(R, 50));
  223. Dlg^.Insert(Control);
  224.  
  225.   R.Assign(2,2,17,3);
  226.   Labl := New(PLabel, Init(R, 'Macro Name  : ', Control));
  227.   Dlg^.Insert(Labl);
  228.  
  229. R.Assign(46,4,54,6);
  230. Control := New(PButton, Init(R, ' OK ', cmOK, bfDefault));
  231. Dlg^.Insert(Control);
  232.  
  233. R.Assign(57,4,67,6);
  234. Control := New(PButton, Init(R, 'Cancel', cmCancel, bfNormal));
  235. Dlg^.Insert(Control);
  236.  
  237. Dlg^.SelectNext(False);
  238. MakeDialog := Dlg;
  239. end;
  240.  
  241. var
  242.   DataRec : record
  243.     Name : String[50]; {Inputline}
  244.     end;
  245.  
  246.  
  247.  
  248. (* ---------------------------- MacroStack ------------------------------- *)
  249.  
  250. procedure TMacroStack.PushMacro (Macro : PMacro);
  251. var P : PMacro;
  252. begin
  253.   new (P);
  254.   P := Macro;
  255.   Push(P);
  256. end;
  257.  
  258. (* ---------